package com.cs.archivesmanagement.action;

import com.cs.archivesmanagement.entity.*;
import com.cs.archivesmanagement.service.AccountService;
import com.cs.archivesmanagement.service.ArchivesDealWithService;
import com.cs.archivesmanagement.service.ArchivesService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.io.*;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * Created by amo on 2015/8/25.
 */

@Controller
@Namespace("/")
@Results({
        @Result(name = "queryarchivessuccess", location = "/b&sManager/filecheck.jsp"),
        @Result(name = "loginError", location = "/index.jsp"),
        @Result(name = "querymyarchivessuccess", location = "/fileManager/myarchives.jsp"),
        @Result(name = "upLoadSuccess", location = "/uploadresult.jsp"),
        @Result(name = "addArvhice", location = "/fileManager/archive_collect.jsp"),
        @Result(name = "archiveesummit", location = "/fileManager/myarchives.jsp"),
        @Result(name = "getArchivePage", location = "/b&sManager/apply.jsp"),
        @Result(name = "getarchivefinish", location = "/b&sManager/filecheck.jsp"),
        @Result(name = "getmygets", location = "/b&sManager/lentrecord.jsp")



})
public class ArhchivesAction extends ActionSupport {

    ArrayList<AmArchives> amArchivesArrayList;

    ArrayList<AmDepartment> amDepartmentArrayList;

    ArrayList<AmFileType> amFileTypeArrayList;

    AmArchives amArchives;

    Integer amArchivesId;

    AmAccount amAccount;

    AmArchivesRequests amArchivesRequests;

    ArrayList<AmArchivesRequests> amArchivesRequestsArrayList;

    @Resource
    ArchivesService archivesService;

    @Resource
    AccountService accountService;


    @Resource
    ArchivesDealWithService archivesDealWithService;




        Integer departmentId;
        Integer typeId;
        String keyWord;
        Integer accountId;
        Integer archiveStatus;

        Integer lentsdays;


    @org.apache.struts2.convention.annotation.Action("queryarchives")
       public String queryarchive(){

                amArchivesArrayList=archivesService.getAllAmArchivesByParameter(departmentId,typeId,keyWord,null,AmArchives.archivesStatus_vaild);
                amDepartmentArrayList=accountService.getAllAmDepartment();
                amFileTypeArrayList=archivesService.getAllFileType();



                return "queryarchivessuccess";
        }


    @org.apache.struts2.convention.annotation.Action("addarchive")

    public String addArchive(){

        System.out.println("addarchives登陆者的id是"+accountId);

        amDepartmentArrayList=accountService.getAllAmDepartment();
        amFileTypeArrayList=archivesService.getAllFileType();



        return "addArvhice";

    }


    @org.apache.struts2.convention.annotation.Action("addarchiveesummit")
    public String addArchiveSummit(){

         upload();

        System.out.println("asdasdasd");
        amArchives.setArchivesDepartmentId(new AmDepartment(1));
        amArchives.setArchivesTypeId(new AmFileType(1));
        amArchives.setArchivesSummittedUserId(new AmAccount(accountId));
        amArchives.setArchivesFilePath(storageFile.toPath().toString());
        archivesService.addArchives(amArchives);

        AmArchivesRequests amArchivesRequests=new AmArchivesRequests();
        amArchivesRequests.setArchivesRequestsUserId(new AmAccount(accountId));
        amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_add);
        amArchivesRequests.setArchivesRequestsArchiveId(amArchives);

        archivesService.addArchiveRequest(amArchivesRequests);

        return "archiveesummit";

    }



    @org.apache.struts2.convention.annotation.Action("querymyarchives")
    public String queryMyArchive(){

        amArchivesArrayList=archivesService.getAllAmArchivesByParameter(departmentId,typeId,keyWord,accountId,archiveStatus);
        amDepartmentArrayList=accountService.getAllAmDepartment();
        amFileTypeArrayList=archivesService.getAllFileType();

        return "querymyarchivessuccess";

    }

    @org.apache.struts2.convention.annotation.Action("getarchiveapply")
    public String getArchivePage(){


        amArchives=archivesService.findById(amArchivesId);
        amAccount=accountService.findById(accountId);


        return "getArchivePage";
    }


    @org.apache.struts2.convention.annotation.Action("getarchivefinish")
   public String getArchiveFinish(){


        amArchives=archivesService.findById(amArchivesId);
        amAccount=accountService.findById(accountId);

        AmArchivesRequests amArchivesRequests=new AmArchivesRequests();
        amArchivesRequests.setArchivesRequestsUserId(amAccount);
        amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_get);
        amArchivesRequests.setArchivesRequestsArchiveId(amArchives);


        System.out.println("借" + lentsdays + "天");
        amArchivesRequests.setArchivesRequestsArchiveEndTime(new Timestamp(amArchivesRequests.getArchivesRequestsArchiveTime().getTime()+lentsdays*24*3600*1000));


        archivesService.addArchiveRequest(amArchivesRequests);

        amArchivesArrayList=archivesService.getAllAmArchivesByParameter(departmentId,typeId,keyWord,null,AmArchives.archivesStatus_vaild);
        amDepartmentArrayList=accountService.getAllAmDepartment();
        amFileTypeArrayList=archivesService.getAllFileType();

    return "getarchivefinish";
   }


    @org.apache.struts2.convention.annotation.Action("getmygets")
    public String getMyPriority(){

        amArchivesRequestsArrayList=archivesDealWithService.getAllAmArchivesRequestsByParameter(AmArchivesRequests.archivesRequestsType_get,accountId,null);
        return "getmygets";

    }




        public ArrayList<AmArchives> getAmArchivesArrayList() {
                return amArchivesArrayList;
        }

        public void setAmArchivesArrayList(ArrayList<AmArchives> amArchivesArrayList) {
                this.amArchivesArrayList = amArchivesArrayList;
        }


        public ArchivesService getArchivesService() {
                return archivesService;
        }

        public Integer getAccountId() {
                return accountId;
        }

        public Integer getArchiveStatus() {
                return archiveStatus;
        }

        public Integer getDepartmentId() {
                return departmentId;
        }

        public Integer getTypeId() {
                return typeId;
        }

        public String getKeyWord() {
                return keyWord;
        }

        public void setAccountId(Integer accountId) {
                this.accountId = accountId;
        }

        public void setArchivesService(ArchivesService archivesService) {
                this.archivesService = archivesService;
        }

        public void setArchiveStatus(Integer archiveStatus) {
                this.archiveStatus = archiveStatus;
        }

        public void setDepartmentId(Integer departmentId) {
                this.departmentId = departmentId;
        }

        public void setKeyWord(String keyWord) {
                this.keyWord = keyWord;
        }

        public void setTypeId(Integer typeId) {
                this.typeId = typeId;
        }

    public ArrayList<AmDepartment> getAmDepartmentArrayList() {
        return amDepartmentArrayList;
    }

    public void setAmDepartmentArrayList(ArrayList<AmDepartment> amDepartmentArrayList) {
        this.amDepartmentArrayList = amDepartmentArrayList;
    }

    public ArrayList<AmFileType> getAmFileTypeArrayList() {
        return amFileTypeArrayList;
    }

    public void setAmFileTypeArrayList(ArrayList<AmFileType> amFileTypeArrayList) {
        this.amFileTypeArrayList = amFileTypeArrayList;
    }

    public AccountService getAccountService() {
        return accountService;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }



    private static final long serialVersionUID = 572146812454l;
    private static final int BUFFER_SIZE = 16 * 1024;
    // 封装上传文件域的属性
    private File upload;
    // 封装上传文件类型的属性
    private String contentType;
    // 封装上传文件名的属性
    private String fileName;
    private String storageFileName;

    private  File storageFile;

    public File getStorageFile() {
        return storageFile;
    }

    public void setStorageFile(File storageFile) {
        this.storageFile = storageFile;
    }

// private String storagePath;

    // since we are using <s:file name="upload" ... /> the File itself will be
    // obtained through getter/setter of <file-tag-name>
    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    // since we are using <s:file name="upload" .../> the file name will be
    // obtained through getter/setter of <file-tag-name>FileName
    public String getUploadFileName() {
        return fileName;
    }

    public void setUploadFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getStorageFileName() {
        return storageFileName;
    }

    public void setStorageFileName(String storageFileName) {
        this.storageFileName = storageFileName;
    }

    // since we are using <s:file name="upload" ... /> the content type will be
    // obtained through getter/setter of <file-tag-name>ContentType
    public String getUploadContentType() {
        return contentType;
    }

    public void setUploadContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void copy(File src, File dst) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(src),
                        BUFFER_SIZE);
                out = new BufferedOutputStream(new FileOutputStream(dst),
                        BUFFER_SIZE);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (in.read(buffer) > 0) {
                    out.write(buffer);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getExtention(String fileName) {
        int pos = fileName.lastIndexOf(".");
        return fileName.substring(pos);
    }



    public void  upload(){

        storageFileName = fileName;
        System.out.println("FileName: " + fileName);
        System.out.println("ContentType: " + contentType);
        System.out.println("File: " + upload);

        storageFile = new File(ServletActionContext.getServletContext()
                .getRealPath("/upload") + "/" + storageFileName);
        copy(upload, storageFile);

        System.out.println(storageFile.toPath().toString());

        System.out.println("File upload");


    }

    public AmArchives getAmArchives() {
        return amArchives;
    }

    public void setAmArchives(AmArchives amArchives) {
        this.amArchives = amArchives;
    }

    public Integer getAmArchivesId() {
        return amArchivesId;
    }

    public void setAmArchivesId(Integer amArchivesId) {
        this.amArchivesId = amArchivesId;
    }

    public AmAccount getAmAccount() {
        return amAccount;
    }

    public void setAmAccount(AmAccount amAccount) {
        this.amAccount = amAccount;
    }

    public Integer getLentsdays() {
        return lentsdays;
    }

    public void setLentsdays(Integer lentsdays) {
        this.lentsdays = lentsdays;
    }

    public AmArchivesRequests getAmArchivesRequests() {
        return amArchivesRequests;
    }

    public void setArchivesDealWithService(ArchivesDealWithService archivesDealWithService) {
        this.archivesDealWithService = archivesDealWithService;
    }

    public void setAmArchivesRequests(AmArchivesRequests amArchivesRequests) {
        this.amArchivesRequests = amArchivesRequests;
    }

    public ArchivesDealWithService getArchivesDealWithService() {
        return archivesDealWithService;
    }

    public ArrayList<AmArchivesRequests> getAmArchivesRequestsArrayList() {
        return amArchivesRequestsArrayList;
    }

    public void setAmArchivesRequestsArrayList(ArrayList<AmArchivesRequests> amArchivesRequestsArrayList) {
        this.amArchivesRequestsArrayList = amArchivesRequestsArrayList;
    }


}
